﻿@model IEnumerable<JewererBasic.ORDER>


@{
    ViewBag.Title = "Order Management";

    var gd = new WebGrid(
            Model,
            canPage: true,
            rowsPerPage: 10,
            selectionFieldName: "selectedRow",
            ajaxUpdateContainerId: "gridContent"
        );
    gd.Pager(WebGridPagerModes.NextPrevious);
}

<h2>Order Management</h2>

@using (Html.BeginForm("Index", "Order", FormMethod.Post, new { @id = "formSearch" }))
{
    <fieldset>
        <p>
            <a href="/Order/CreateNewOrder">
                <input type="button" value="CREATE" title="Click this button to create new order." />
            </a>
        </p>

        <table>
            <tr>
                <td>
                    <fieldset>
                        <fieldset>
                            <legend>SEARCH</legend>
                            <table>
                                <tr>
                                    <td style="text-align: center;"><b>Order Id</b></td>
                                    <td style="text-align: center;"><b>Customer Id</b></td>
                                    <td style="text-align: center;"><b>Start Week</b></td>
                                    <td style="text-align: center;"><b>Finish Week</b></td>
                                </tr>
                                <tr>
                                    <td>@Html.TextBox("tbIdOrderSearch", "", new { title = "Input order id to filter.", placeholder = "Order Id", onkeypress = "submitonenter('formlogin',event,this)" })</td>
                                    <td>@Html.TextBox("tbIdCustomerSearch", "", new { title = "Input customer id to filter.", placeholder = "Customer Id", onkeypress = "submitonenter('formlogin',event,this)" })</td>
                                    <td>@Html.TextBox("tbStartWeekSearch", "", new { type = "number", min = "0", @class = "inputItem", title = "Input start week to filter.", placeholder = "Start week", onkeypress = "submitonenter('formlogin',event,this)" })</td>
                                    <td>@Html.TextBox("tbFinishWeekSearch", "", new { type = "number", min = "0", @class = "inputItem", title = "Input finish week to filter.", placeholder = "Finish week", onkeypress = "submitonenter('formlogin',event,this)" })</td>
                                    <td><input type="submit" name="submit" value="SEARCH" title="Click this button to search." /></td>
                                </tr>
                            </table>
                        </fieldset>

                        @if (Model.Count() > 0)
                        {
                            <div title="This area for orders details.">
                                @gd.GetHtml(
                            tableStyle: "table",
                                headerStyle: "head",
                                rowStyle: "row",
                                alternatingRowStyle: "altRow",
                                selectedRowStyle: "selectRow",
                                columns: gd.Columns(
                                gd.Column(header: "Order Id", format: @<div style="width: 150px; text-align: center;"><a href="@Url.Action("Details", new { id = item.IdOrder })">@item.IdOrder</a></div>),
                                gd.Column(header: "Customer Id", format: @<div style="width: 150px; text-align: center;">@item.IdCustomer</div>),
                                gd.Column(header: "Start Week", format: @<div style="width: 100px; text-align: center;">@item.StartWeek</div>),
                                gd.Column(header: "Finish Week", format: @<div style="width: 100px; text-align: center;">@item.FinishWeek</div>),
                                gd.Column(header: "Finished Bags", format: @<div style="width: 100px; text-align: center;">@((item.TotalFinish == null) ? "0" : item.TotalFinish.ToString()) / @(item.TotalBags == null ? "0" : item.TotalBags.ToString())</div>),
                                gd.Column(header: "Action", format: @<div style="width: 100px; text-align: center;">@Html.ActionLink("Details", "Details", new { id = item.IdOrder }, new { title = "Show details for order: " + @item.IdOrder + "." })</div>)
                            ))
                            </div>
                        }
                        else
                        {
                            <div id="noResult">There is no suitable result.</div>
                        }
                    </fieldset>
                </td>
            </tr>
        </table>
    </fieldset>
}

@section Scripts{
    <script type="text/javascript">

        function submitonenter(formid, evt, thisObj) {
            evt = (evt) ? evt : ((window.event) ? window.event : "")
            if (evt) {
                // process event here
                if (evt.keyCode == 13 || evt.which == 13) {
                    document.getElementById(formid).submit();
                }
            }
        }

        jQuery(".inputItem").keydown(function (event) {
            // Allow: backspace, delete, tab, escape, enter and .
            if (jQuery.inArray(event.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
                // Allow: Ctrl+A
                (event.keyCode == 65 && event.ctrlKey === true) ||
                // Allow: home, end, left, right
                (event.keyCode >= 35 && event.keyCode <= 40)) {
                // let it happen, don't do anything
                return;
            }
            else {
                // Ensure that it is a number and stop the keypress
                if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                    event.preventDefault();
                }
                else {
                    var $focused = $(':focus');
                    if ($focused.val().length >= 10) {
                        $focused.keydown(function (event) {
                            event.preventDefault();
                        })
                    }
                }
            }
        });
    </script>
}